// pages/new_district/new_district.js
var app = getApp()
var header = {
  'content-type': 'application/x-www-form-urlencoded',
  'cookie': "openid=" + app.globalData.openid
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    district:'',
    describe:'',
    name:'',
    telephone:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  formSubmit: function (e) {
    let that = this;   

    that.setData({
      district: e.detail.value.input_district,
      describe: e.detail.value.textarea,
      name: e.detail.value.input_name,
      telephone: e.detail.value.input_telephone
    });
   
    wx.request({
      url: 'https://119.3.46.145/server/addDepartment?userid=' + app.globalData.userid + '&name=' + that.data.district + '&description=' + that.data.describe + "&admin=" + that.data.name+'&phone='+that.data.telephone,
      method: 'POST',
      header: header,
      success: res => {
        //data: { error: "0", info: "添加成功" }
        
        let result = res.data;

        if(result.error ==  "0"){
          wx.showModal({
            title: '提示',
            content: result.info,
            success:function(){
              wx.request({
                url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid,  //获取区域列表构造树
                header: header,
                success: function (res) {
                  // console.log(res, "res");
                  //[{"attributes":{"mid":1,"type":0},"id":100000001,"text":"测试区域2","state":"closed","iconCls":"area_img"}]
                  let data = JSON.parse(res.data.data);

                  //当前没有区域 []
                  if (data.length == 0) {
                    return;
                  }

                  //当前有区域[{"attributes":{"mid":1,"type":0},"id":100000001,"text":"测试区域2","state":"closed","iconCls":"area_img"}]
                  let tmp_distrcit = [];
                  data.forEach(function (item, index, self) {
                    let child_district = {
                      text: '',
                      mid: '',
                      type: '',
                      id: '',
                      state: ''
                    };
                    child_district.text = item.text;
                    child_district.mid = item.attributes.mid;
                    child_district.type = item.attributes.type;
                    child_district.id = item.id;
                    child_district.state = item.state;
                    tmp_distrcit.push(child_district);
                  });

                  let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
                  let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值
                  prevPage.setData({
                    tree_district: tmp_distrcit
                  });
                  wx.navigateBack(); //关闭当前页面，返回上一个页面
                },
                fail: function (error) {
                  console.log(error, "dev_manager  onload loadTree");
                }
              });
            }
          });

        

         
        }
      },
      fail: function (error) {
        console.log(error, "addDepartment error");
      }
    })
  },

  formReset: function (e) {
    let that = this;
    that.setData({
      district:'',
      describe: '',
      name: '',
      telephone:''
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})